<?php
namespace app\admin\controller;

use app\admin\model\SmsTemplate as SmsTemplateModel;
use app\common\controller\AdminBaseController;

/**
 * 短信模板管理
 * Class SmsTemplate
 * @package app\admin\controller
 */
class SmsTemplateController extends AdminBaseController {
	
    protected $sms_model;

    protected function _initialize() {
        parent::_initialize();
        $this->sms_model = new SmsTemplateModel();
    }

    /**
     * 列表
     * @return mixed
     */
    public function index() {
        $list = $this->sms_model->order('update_time desc')->paginate(15);
        return $this->fetch('index', ['list' => $list]);
    }

    /**
     * 添加
     * @return mixed
     */
    public function add() {
    	if (request()->isPost()) {
            $data = input();
            $validate_result = $this->validate($data, 'SmsTemplate');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->sms_model->allowField(true)->save($data)) {
                    $this->success('添加成功');
                } else {
                    $this->error('添加失败');
                }
            }
        }else{
        	return $this->fetch();
        }
    }

    /**
     * 更新
     * @param $id
     * @param $group_id
     */
    public function update($id) {
        if ($this->request->isPost()) {
            $data = input();
            if ($this->sms_model->allowField(true)->save($data,['id'=>$id]) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }else{
        	$model = $this->sms_model->find($id);
     		return $this->fetch('update', ['model' => $model]);
        }
    }

    /**
     * 删除
     * @param $id
     */
    public function delete($id, $ids = []) {
    	$id = $ids ? $ids : $id;
        if ($this->sms_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
